package com.tensquare.user.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.tensquare.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;

/**
 * 服务层
 *
 * @author Administrator
 */
@Service
public class AdminService
{

    private final BCryptPasswordEncoder encoder;
    private final AdminDao adminDao;
    private final IdWorker idWorker;


    @Autowired
    public AdminService(BCryptPasswordEncoder encoder, AdminDao adminDao, IdWorker idWorker)
    {
        this.encoder = encoder;
        this.adminDao = adminDao;
        this.idWorker = idWorker;
    }

    /**
     * 查询全部列表
     *
     *
     */
    public List<Admin> findAll()
    {
        return adminDao.findAll();
    }


    public Admin findByLoginNameAndPassword(String loginname, String password)
    {
        Admin admin = adminDao.findByLoginname(loginname);
        // BCrypt验证
        if (admin != null && encoder.matches(password, admin.getPassword()))
        {
            return admin;
        } else
        {
            return null;
        }
    }

    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Admin> findSearch(Map whereMap, int page, int size)
    {
        Specification<Admin> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return adminDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Admin> findSearch(Map whereMap)
    {
        Specification<Admin> specification = createSpecification(whereMap);
        return adminDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Admin findById(String id)
    {
        return adminDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param adminInfo
     */
    public boolean add(Admin adminInfo)
    {
        Admin admin = adminDao.findByLoginname(adminInfo.getLoginname());
        if (admin != null)
        {
            return false;
        }
        adminInfo.setId(String.valueOf(idWorker.nextId()));
        String newPassword = encoder.encode(adminInfo.getPassword());
        adminInfo.setPassword(newPassword);
        adminDao.save(adminInfo);
        return true;
    }

    /**
     * 修改
     *
     * @param admin
     */
    public void update(Admin admin)
    {
        adminDao.save(admin);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id)
    {
        adminDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Admin> createSpecification(Map searchMap)
    {

        return new Specification<Admin>()
        {

            @Override
            public Predicate toPredicate(Root<Admin> root, CriteriaQuery<?> query, CriteriaBuilder cb)
            {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                {
                    predicateList.add(cb.like(root.get("id").as(String.class), "%" + (String) searchMap.get("id") + "%"));
                }
                // 登陆名称
                if (searchMap.get("loginname") != null && !"".equals(searchMap.get("loginname")))
                {
                    predicateList.add(cb.like(root.get("loginname").as(String.class), "%" + (String) searchMap.get("loginname") + "%"));
                }
                // 密码
                if (searchMap.get("password") != null && !"".equals(searchMap.get("password")))
                {
                    predicateList.add(cb.like(root.get("password").as(String.class), "%" + (String) searchMap.get("password") + "%"));
                }
                // 状态
                if (searchMap.get("state") != null && !"".equals(searchMap.get("state")))
                {
                    predicateList.add(cb.like(root.get("state").as(String.class), "%" + (String) searchMap.get("state") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

}
